*===============================================================================
*
*					WORKER BELIEFS ABOUT OUTSIDE OPTIONS
*		(c)	Simon Jaeger, Christopher Roth, Nina Roussille, Benjamin Schoefer
*							  2023 December 5
*						   	   SOEP-IAB Data 
*
*===============================================================================


********************************************************************************
*								Figure B07 abcd								   *
********************************************************************************

cap log close      
log using ${log}/FigureB07abcd.log, replace       

use "$data/FTp_u__finaldata.dta", clear



*** variable cleaning for these analyses
		
* looping over the list of behaviors
foreach behavior in switchout_proba negotiate_proba negotiate_pct_c  paycut_quit_pct {
			
	* setting y-axis titles and filename suffixes						
	if "`behavior'"=="switchout_proba" { // Figure B07 a
		local ytitle "Probability of Seeking New Job"
		local behsuffix "search"
	}
	if "`behavior'"=="paycut_quit_pct" { // Figure B07 b
		local ytitle "Reservation Wage Cut"
		local behsuffix "rescut"
	}	
	if "`behavior'"=="negotiate_proba" { // Figure B07 c
		local ytitle "Probability of Negotiating for Wage Raise"
		local behsuffix "bargain"
	}
	if "`behavior'"=="negotiate_pct_c" { // Figure B07 d
		local ytitle "Wage Raise Magnitude"
		local behsuffix "bmagc"
	}

	local xtitle "Belief About Wage Change if Moving to Outside Option"
						
	* calculating coefficients
	reg `behavior' ln_own_change_w if p_delta_ln_wage_mover!=., cluster(pid)
		local beh_beta1 = string(round(_b[ln_own_change_w],0.001),"%4.3f")
		local beh_se1 = string(round(_se[ln_own_change_w],0.001),"%4.3f")
		local beh_constant1 = string(round(_b[_cons],0.001),"%4.3f")
		local beh_N1 = e(N)
				
	reg `behavior' ln_own_change_w p_delta_ln_wage_mover if p_delta_ln_wage_mover!=., cluster(pid)
		local beh_beta2 = string(round(_b[ln_own_change_w],0.001),"%4.3f")
		local beh_se2 = string(round(_se[ln_own_change_w],0.001),"%4.3f")
		local beh_constant2 = string(round(_b[_cons],0.001),"%4.3f")
		local beh_N2 = e(N)
				
	* partialling out the control variable(s), to prepare for plotting
	reg `behavior' p_delta_ln_wage_mover if p_delta_ln_wage_mover!=.
		predict r_`behavior' if p_delta_ln_wage_mover!=., resid
		quietly su `behavior'
		replace r_`behavior' = r_`behavior'+r(mean)
				
	reg ln_own_change_w p_delta_ln_wage_mover if p_delta_ln_wage_mover!=.
		predict r_ln_own_change_w if p_delta_ln_wage_mover!=., resid
		quietly su ln_own_change_w
		replace r_ln_own_change_w = r_ln_own_change_w+r(mean)
				
	reg r_`behavior' r_ln_own_change_w if p_delta_ln_wage_mover!=.
		local beh_constant2 = string(round(_b[_cons],0.001),"%4.3f") 
				
	* creating plots
	local splitvar "dup"
				
	preserve
				
		expand 2, gen(dup)
						
		replace `behavior' = r_`behavior' if dup==1
		replace ln_own_change_w = r_ln_own_change_w if dup==1
				
		binscatter `behavior' ln_own_change_w if p_delta_ln_wage_mover!=., by(`splitvar') savedata("$data/binscdata") replace reportreg
				
	restore
				
	preserve
	
		clear
					
		quietly do "$data/binscdata.do"
					
		quietly su ln_own_change_w_by1
		local xmin = r(min)
		local xmax = r(max)
					
		quietly su ln_own_change_w_by2
		if r(min)<`xmin' local xmin = r(min)
		if r(max)>`xmax' local xmax = r(max)
					
		local xmin = -0.3 
		local xmax = 0.8 
					
		if "`behavior'"!="negotiate_pct" & "`behavior'"!="negotiate_pct_c" & "`behavior'"!="ln_salary" & "`behavior'"!="ln_salary_d" & "`behavior'"!="ln_salary_d0" & "`behavior'"!="ln_salary_d1" {
			local ymin = 0
			local ymax = 0.6
		} 
		if "`behavior'"=="paycut_quit_pct" {
			local ymin = 0.1
			local ymax = 0
		}
		if "`behavior'"=="negotiate_pct_c" {
			local ymin = 0
			local ymax = 0.25
		}
										
		
		twoway scatter `behavior'_by1 ln_own_change_w_by1, mcolor(navy) msymbol(circle) ///
			|| scatter `behavior'_by2 ln_own_change_w_by2, mcolor(maroon) msymbol(Th) ///
			|| function y=(`beh_beta1'*x)+`beh_constant1', range(`xmin' `xmax') color(navy) ///
			|| function y=(`beh_beta2'*x)+`beh_constant2', range(`xmin' `xmax') color(maroon) lpattern(dash) /*
			*/ legend(order(1 "Uncontrolled, Slope: `beh_beta1' (SE `beh_se1')" 2 "Controlling for Objective Benchmark, Slope: `beh_beta2' (SE `beh_se2')") cols(1) ring(0) size(small) position(11) region(lwidth(none)))   ///
			ytitle(`ytitle') xtitle(`xtitle') ylabel(#10,grid) xlabel(#10,grid) xscale(range(`xmin' `xmax')) yscale(range(`ymin' `ymax'))
			
			
		if "`behavior'"=="switchout_proba" { // Figure B07 a
			graph export ${log}/FigureB07a.pdf, replace
		}
		if "`behavior'"=="paycut_quit_pct" { // Figure B07 b
			graph export ${log}/FigureB07b.pdf, replace
		}		
		if "`behavior'"=="negotiate_proba" { // Figure B07 c
			graph export ${log}/FigureB07c.pdf, replace
		}		
		if "`behavior'"=="negotiate_pct_c" { // Figure B07 d
			graph export ${log}/FigureB07d.pdf, replace
		}
					
	restore
			
	cap erase "$data/binscdata.do" 
	cap erase "$data/binscdata.csv"
		
	drop r_`behavior' r_ln_own_change_w
				
}
									
					
		
		
cap log close
clear

